// index.js
// 获取应用实例
const app = getApp()
import {
    getUserInfoByUserId,
    getProject,
    placeFile,
    setProjectName,
    getProjectList,
    alterPayProjectList,
    joinProject,
    workInvitationLeader
} from '../../../api/app.js'
import user from '../../../utils/user.js'
import {shake} from '../../../utils/util'
Page({
    data: {
        tabs: [
            '全部',
            '无打卡',
            '需打卡'
        ],
        inviteRegisterStatus: false,
        invite: '',
        top: app.globalData.menuTop,
        showMoreStatus: false,
        morePosition: 0,
        current: 0,
        addProjectStatus: false,
        projectList: [],
        videoStatus: false,
        isAdmin: false,
        hasNext: true,
        pageNum: 1,
        noBuyUser: false,
        setProjectNameId: false,
        setProjectNameIndex: false,
        searchText:''
    },
    static:{
        payAllList:[],
        payAllListNum:0,
        payDayList:[],
        payDayListNum:0,
        payMonthList:[],
        payMonthListNum:0
    },
    dateChange(time) {
        time = time.detail
        this.setData({
            day: time
        })
    },
    onShow() {
        // this.getTabBar().setData({
        //     selected: 0
        // })
        user.waitLogin().then(() => {
            this.setData({
                isAdmin: user.isAdmin,
                'companyName': user.companyName,
                noBuyUser: !user.auditStatus,
            })
            this.getProjectList(true)
        })
    },
    switchtab(event) {
        if (!user.userId) {
            app.confirm('您还未登录，是否立即登录？').then(() => {
                user.goRegister()
            })
            return
        }
        this.setData({
            current: event.currentTarget.dataset.index,
            pageNum: 1,
            // hasNext: true
        })
        this.alterPayProjectList()
    },
    /**
     * 检测列表是否发生改变
     */
    alterPayProjectList(){
        alterPayProjectList({
            type: (this.data.current ? (this.data.current - 1) : ''),
            searchName:this.data.searchText
        }).then(res=>{
            console.log(res,'检测')
            console.log(this.data.current,'current')
            switch(this.data.current){
                case 0:
                    if(this.static.payAllListNum == res){
                        this.setData({
                            projectList:this.static.payAllList
                        })
                    }else{
                        this.getProjectList(true,this.data.current ? (this.data.current - 1) : null)
                    }
                    break
                case 1:
                    if(this.static.payDayListNum == res){
                        this.setData({
                            projectList:this.static.payDayList
                        })
                    }else{
                        this.getProjectList(true,this.data.current ? (this.data.current - 1) : null)
                    }
                    break
                case 2:
                    if(this.static.payMonthListNum == res){
                        this.setData({
                            projectList:this.static.payMonthList
                        })
                    }else{
                        this.getProjectList(true,this.data.current ? (this.data.current - 1) : null)
                    }
                    break
            }
        })
    },
    // 显示更多
    showMore(event) {
        const list = [{
            text: '电话',
            key: event.currentTarget.dataset.phone,
            icon: '',
            bind: 'call'
        },
        {
            text: '标记大神',
            key: event.currentTarget.dataset.index,
            icon: '',
            bind: 'mark'
        }]
        // 如果是老项目，不允许删除员工
        if (!this.data.isOld) {
            list.push({
                text: '删除',
                key: event.currentTarget.dataset.index,
                icon: '',
                bind: 'delete'
            })
        }
        this.setData({
            'moreList': list
        })
        this.setData({
            morePosition: event.touches[0].clientY + 6,
            showMoreStatus: true
        })
    },
    // 跳转到登录
    login() {
        user.goRegister()
    },
    hideMore() {
        this.setData({
            showMoreStatus: false
        })
    },
    onPcPay(){
        wx.navigateTo({
            url: '/packagePay/pages/pc-pay/pc-pay'
        })
    },
    onPayRecord(){
        wx.navigateTo({
            url: '/packagePay/pages/bill/bill'
        })
    },
    onFastPay(){
        if(user.quickProject){
            wx.navigateTo({
                url: '/packagePay/pages/fast-pay/fast-pay'
            })
        }
        else{
            wx.navigateTo({
                url: '/packagePay/pages/fast-pay-guide/fast-pay-guide'
            })
        }

    },
    setProjectName(event){
        this.setData({
            setProjectNameId: event.detail,
            setProjectNameIndex: event.currentTarget.dataset.index
        })
    },
    onCancelProjectName(){
        this.setData({
            setProjectNameId: false,
            setProjectNameIndex: false
        })
    },
    onSetProjectName(event){
        if(!event.detail.name){
            app.toast('请输入项目名称')
            return 
        }
        setProjectName({
            id: this.data.setProjectNameId,
            projectName: event.detail.name
        }).then(()=>{
            app.toast('修改成功','success')
            this.setData({
                setProjectNameId: false,
                ['projectList['+this.data.setProjectNameIndex+'].projectName']: event.detail.name
            })
        })
    },
    // 获取当前用户日程
    getProjectList(first,tabsIndex = null) {
        if(first){
            this.data.pageNum = 1
            this.data.hasNext = true
        }
        console.log(this.data.searchText,'this.data.searchText')
        getProjectList({
            companyId: user.companyId,
            type: this.data.current ? (this.data.current - 1) : null,
            params: {
                userId: user.userId
            },
            pageNum: this.data.pageNum,
            pageSize:50,
            searchName:this.data.searchText
        }).then((res) => {
            console.log(this.data.searchText,'this.data.')
            if(!res.hasNextPage){
                this.setData({
                    hasNext: false
                })
            }else{
                this.data.hasNext = true
            }
            if(first){
                this.data.projectList = []
            }
            const projectList = this.data.projectList
            projectList.push(...res.list)
            console.log(tabsIndex,'tabsIndex')
            switch(tabsIndex){
                case null:
                    this.static.payAllList = projectList
                    this.static.payAllListNum = res.totalCount
                    break
                case 0:
                    this.static.payDayList = projectList
                    this.static.payDayListNum = res.totalCount
                    break
                case 1:
                    this.static.payMonthList = projectList
                    this.static.payMonthListNum = res.totalCount
                    break
            }
            this.setData({
                projectList :tabsIndex == null ? this.static.payAllList : tabsIndex == 0 ? this.static.payDayList : this.static.payMonthList
            })
        })
    },
    onScroll() {
        console.log(this.data.hasNext,'hasNext')
        if(!this.data.hasNext){
            return 
        }
        this.setData({
            pageNum: this.data.pageNum + 1
        })
        this.getProjectList()
    },
    // 创建项目成功
    appProjectComplete(event) {
        this.hideAddProject()
        app.toast('创建项目成功，正在为您跳转')
        setTimeout(() => {
            this.getProjectList()
            wx.navigateTo({
                url: '/packagePay/pages/resident/resident?id=' + event.detail
            })
        }, 2000)
    },

    // 归档项目
    onPlaceFile(event) {
        wx.showModal({
            title: '项目归档',
            content: '项目归档后不会影响其他项目成员查看项目，如需查看可至“工作台-归档项目”中查看',
            confirmColor: '#00D679',
            confirmText: '确定归档',
            success: (res) => {
                if (res.confirm) {
                    placeFile({
                        id: event.detail,
                        isArchived: 1
                    }).then(() => {
                        app.toast('项目归档成功', 'success')
                        this.data.projectList.splice(event.currentTarget.dataset.index,1)
                        this.setData({
                            projectList: this.data.projectList
                        })
                            
                    
                    })
                }  
            } 
        })
    },

    // 前往项目详细页面
    goResident(event) {
        console.log(event)
        wx.navigateTo({
            url: '/packagePay/pages/resident/resident?id=' + event.currentTarget.dataset.id
        })
    },

    hideAddProject() {
        this.setData({
            addProjectStatus: false
        })
    },
    showAddProject() {
        if (!user.userId) {
            app.confirm('您还未登录，是否立即登录？').then(() => {
                user.goRegister()
            })
            return 0
        }
        this.setData({
            addProjectStatus: true
        })
    },
    completeSubscription() {
        this.setData({
            showSubscription: false
        })
    },
    onShareAppMessage() {
        return {
            title: user.userName + '邀请您一起使用子弹发薪|无卡发薪快一步',
            imageUrl: '/assets/img/home/share.png'
        }
    },
    onShareTimeline() {
        return {
            title: '我在使用【子弹发薪 | 无卡发薪快一步】',
            imageUrl: '/assets/img/home/share.png'
        }
    },
    showVideo() {
        this.setData({
            videoStatus: true
        })
    },
    closeVideo() {
        app.toast('感谢您的观看，现在开始体验吧！')
        this.setData({
            videoStatus: false
        })
    },
    /**
     * 根据项目名称搜索
     */
    searchWorker(event){
        shake(()=>{
            const { value } = event.detail
            console.log(value,'value')
            this.setData({
                searchText:value,
                projectList:[],
                pageNum:1,
                // hasNext:true
            })
            this.alterPayProjectList()
        })
    }
})